<?php

namespace app\api\controller;

use app\common\model\Order as OrderModel;
use app\common\model\Staff;
use app\common\controller\Api;
use app\common\model\Staff as StaffModel;
use app\common\model\Stat;
use app\common\model\Store as StoreModel;
use app\common\model\Suborder;

/**
 * 门店接口
 */
class Store extends Api
{
    //protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 门店列表
     *
     */
    public function lists()
    {
        $store_id = input('store_id','');
        if (empty($store_id)){
            $this->error('缺少参数门店id');
        }
        $list = StoreModel::where('store_id','in',$store_id)
            ->field('store_id,store_name,mobile,address')->select();
        $this->success('请求成功',$list);
    }

    //门店资料
    public function info()
    {
        $store_id = input('store_id','');
        if (empty($store_id)){
            $this->error('缺少参数门店id');
        }

        $info = StoreModel::get($store_id);
        $this->success('请求成功',$info);
    }

    //修改门店资料
    public function editInfo()
    {
        $store_id = input('store_id','');
        if (empty($store_id)){
            $this->error('缺少参数门店id');
        }
        $store_name = input('store_name',0);
        $image = input('image','');
        $mobile = input('mobile','');
        $address = input('address','');

        $info = StoreModel::get($store_id);
        if ($store_name){
            $info->store_name = $store_name;
        }
        if ($image){
            $info->image = $image;
        }
        if ($mobile){
            $info->mobile = $mobile;
        }
        if ($address){
            $info->address = $address;
        }

        $info->save();
        $this->success('修改成功');
    }

    //功能设置
    public function setting()
    {
        $store_id = input('store_id','');
        if (empty($store_id)){
            $this->error('缺少参数门店id');
        }

        $max_time_subscribe = input('max_time_subscribe',0);
        $expired_time = input('expired_time',0);
        $not_allow_cancel_time = input('not_allow_cancel_time',0);
        $not_allow_subscribe_time = input('not_allow_subscribe_time',0);

        $info = StoreModel::get($store_id);
        $info->max_time_subscribe = $max_time_subscribe;
        $info->expired_time = $expired_time;
        $info->not_allow_cancel_time = $not_allow_cancel_time;
        $info->not_allow_subscribe_time = $not_allow_subscribe_time;
        $info->save();
        $this->success('设置成功');
    }

    //门店下的员工列表
    public function staff()
    {
        $store_id = input('store_id','');
        if (empty($store_id)){
            $this->error('缺少参数门店id');
        }

        $list = \app\common\model\User::where('FIND_IN_SET(:store_id,store_id)',['store_id' => $store_id])
            ->where(['type'=>1])
            ->field('id,username,nickname,mobile,avatar,service_status')
            ->order('createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $stat_model = new Stat();
        foreach ($list as $k=>$v){
            $num = $stat_model->projectNum($v['id']);
            $list[$k]['num'] = $num;
        }
        $this->success('',$list);
    }

    //员工看板
    public function staffList()
    {
        $store_id = input('store_id','');
        if (empty($store_id)){
            $this->error('缺少参数门店id');
        }
        $status = input('service_status','all');

        $where = [];

        if ($status!='all'){
            $where['service_status'] = $status;
        }

        $list = \app\common\model\User::where($where)
            ->where('FIND_IN_SET(:store_id,store_id)',['store_id' => $store_id])
            ->where(['type'=>1])
            ->field('id,username,nickname,mobile,avatar,service_status,work_end_time')
            ->order('createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        // 当前日期
        $today = date('Y-m-d');

        // 当天开始时间戳（00:00:00）
        $startOfDay = strtotime($today);

        // 当天结束时间戳（23:59:59）
        $endOfDay = strtotime($today . ' 23:59:59');

        foreach ($list as $k=>$v){
            //项目数量
            $project_num = OrderModel::where(['staff_id'=>$v['id'],'is_end'=>1])
                ->where('end_time', 'between time', [$startOfDay, $endOfDay])
                ->sum('num');

            //加钟的项目数量
            $suborder_num = Suborder::alias('so')
                ->join('order o','so.order_id = o.order_id')
                ->where(['so.staff_id'=>$v['id'],'o.is_end'=>1])
                ->where('o.end_time', 'between time', [$startOfDay, $endOfDay])
                ->sum('so.num');

            $list[$k]['project_num'] = $project_num+$suborder_num;
        }

        $this->success('',$list);
    }

    //修改员工状态
    public function editStaffStatus()
    {
        $staff_id = input('staff_id','');
        if (empty($staff_id)){
            $this->error('缺少参数员工id');
        }
        $service_status = input('service_status',0);

        $staff_model = new \app\common\model\User();
        $staff_model->edit($staff_id,$service_status);

        $this->success('修改成功');
    }
}
